import {Component, ElementRef, Input, OnInit, Renderer2, ViewChild} from '@angular/core';
import {HttpService} from '../../shared/service/http.service';
import {AppGlobalService} from '../../shared/service/app-global.service';

@Component({
    selector: 'app-news-list',
    templateUrl: './news-list.component.html',
    styleUrls: ['./news-list.component.scss']
})
export class NewsListComponent implements OnInit {
    @Input() tagData: any;
    @ViewChild('list') list: ElementRef;
    sessionId: string;
    newsList: any;

    slideOpts : any;

    constructor(
        private rd2: Renderer2,
        private http: HttpService,
        private appGlobalService: AppGlobalService
    ) {
    }

    ngOnInit() {
        this.newsList = this.tagData.data;
        this.sessionId = localStorage.getItem('sessionId');
        const refreshParam = {
            id: this.tagData.refreshKey,
            content: {},
            sessionId: this.sessionId
        };
        setInterval(()=>{
            this.http.post('application/submit', refreshParam, (v) => {
                this.newsList = v.data.data;
            });
        }, this.tagData.perpareTime);

        this.slideOpts = {
            direction: 'vertical',
            slidesPerView: this.tagData.displayNum,
            spaceBetween: 0,
            centeredSlides: false,
            autoplay: {
                delay: this.tagData.slignTime,
                disableOnInteraction: false,
            }
        };
    }

    goDetail(id) {
        let targetObj = {
            pageId: this.tagData.targetId,
            params: {
                id: id
            },
            sessionId: this.sessionId
        };
        this.appGlobalService.goToForward(targetObj);
    }
}
